﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseData;
using BaseData.Exts;
using BaseData.Models;
using BaseLib.Helper;

namespace WebUI.UserControls.Share
{
    public partial class Mainor : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var flag = ((CompilationSection)WebConfigurationManager.OpenWebConfiguration("/web.config").GetSection("system.web/compilation")).Debug;
            this.PlaceHolderMainor.Controls.Add(this.Page.LoadControl("~/UserControls/Share/Heador.ascx"));
            this.PlaceHolderMainor.Controls.Add(this.Page.LoadControl("~/UserControls/Share/Navigate.ascx"));
            Exception error = null;
            try
            {
                if (!Request.QueryString["folder"].IsNullOrWhiteSpace() && Regex.Match(Request.QueryString["folder"], @"[^/\\]+").Success)
                {
                    // int nodeCount = 0, infoCount = 0;
                    using (var ctx = new ContextExt())
                    {
                        // 第一步,取得当前的NodeID。
                        /// var nodeId = new GetNewsNodeIdByFolder().Get(ctx, Request.QueryString["folder"]);
                        /// //Response.Write("forders:" + Request.QueryString["folder"] + "~");
                        /// if (nodeId.Any())
                        /// {
                        ///     var req = new GetNewsQueryString().Get();
                        ///     if (req.InfoId.Any())//根据reqInfoId确定NodeId
                        ///     {
                        ///         var infos = from q in ctx.Web_Base_News_Info where req.InfoId.Contains(q.PkId) select q.NodeId;
                        ///         if (infos.Any())
                        ///         {
                        ///             nodeId = infos.ToList<int>();
                        ///         }
                        ///         else //未找到当前 Info 记录，错误提示页
                        ///             throw new ArgumentException("未找到当前指定的 InfoID 记录号！！！");
                        ///     }
                        ///     else
                        ///     {
                        ///         nodeId = req.NodeId.Any() ? req.NodeId : nodeId;
                        ///     }
                        ///     // 第二步，根据NodeID，取得Template类型：1.节点下有子节点，取节点页：Node。2.节点下无子节点：（1）有0或1条信息，取内容页：view，有多条信息，取列表页。（3）reqInfoID不为0时，无条件为View。
                        ///     var nodeType = req.InfoId.Any() ? Enums.NodeType.View : new GetNewsNodeType().Get(ctx, nodeId);
                        ///     // 第三步，根据NodeID，递归取得模板。
                        ///     var templateFile = new GetTemplateByNodeType().Get(ctx, nodeType, nodeId);
                        ///     if (flag) this.LabelInfos.Text = (" nodeId:" + nodeId + " nodeType:" + nodeType + " templateFile:" + templateFile);
                        ///     if (!templateFile.IsNullOrWhiteSpace()) this.PlaceHolderMainor.Controls.Add(this.Page.LoadControl(templateFile));
                        /// }
                    }
                }
                else
                {
                    this.PlaceHolderMainor.Controls.Add(this.Page.LoadControl("~/UserControls/Share/FocusAD.ascx"));
                    this.PlaceHolderMainor.Controls.Add(this.Page.LoadControl("~/UserControls/Items/Home/Index.ascx"));
                }
            }
            catch (Exception err)
            {
                error = err;
                //throw new ArgumentException(err.Message);
            }
            if (flag) this.LabelError.Text = string.Format("{0} - {1}", arg0: !error.IsNullOrWhiteSpace() ? error.Message : "", arg1: (Request.QueryString.Count == 0 ? "" : " QueryString:" + Request.QueryString));
            //this.PlaceHolderMainor.Controls.Add(this.Page.LoadControl("~/UserControls/Share/Error.ascx"));
            this.PlaceHolderMainor.Controls.Add(this.Page.LoadControl("~/UserControls/Share/Footor.ascx"));
        }
    }
}